package com.gitee.sentinelservice.service.impl;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.gitee.sentinelservice.service.SentinelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @ClassName SentinelServiceImpl
 * @Description TODO
 * @Author xiongchao
 * @Date 2021/1/5 15:29
 **/
@Slf4j
@Service
public class SentinelServiceImpl implements SentinelService {

    @SentinelResource(value = "limit",defaultFallback = "defaultFallback",blockHandler = "handlerException",blockHandlerClass = {BlockException.class})
    @Override
    public String hello(String limit) {
        return "阀值类型 QPS  流控模式直接";
    }


    @SentinelResource(value = "thread",defaultFallback = "defaultFallback",blockHandler = "handlerException",blockHandlerClass = {BlockException.class})
    @Override
    public String threadTest(String thread) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "阈值类型 线程数、流控模式 直接";
    }


    @Override
    @SentinelResource(value = "degradeRt" ,defaultFallback = "defaultFallback",blockHandler = "handlerException",blockHandlerClass = {BlockException.class})
    public String fuse(String str){
        if(str.equals("fuse")){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        log.info("熔断测试~~~~");
        return "degradeRt =" + str + "当前时间为" + System.currentTimeMillis();
    }


    public String defaultFallback(){
        return "太拥挤了 ~ 请稍后重试 ";
    }

    public String handlerException(){
        return "测试超出流量限制的部分是否会进入到blockHandler的方法。";
    }

}
